package com.knowif.backend.service;

import com.github.pagehelper.PageHelper;
import com.knowif.pojo.StanderSorce;
import com.knowif.pojo.StanderSorceExample;
import com.knowif.service.StanderSorce_service;
import com.knowif.util.commonUtil.ObjectUtil;
import com.knowif.util.commonUtil.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：Xh
 * @date ：Created in 2019/6/5 0005 20:58
 */
@Service
public class StanderSorceService {

    @Autowired
    private StanderSorce_service standerSorce_service;

    /**
     * 条件查询standerSorce
     * @param standerSorceExample
     * @return
     */
    public List<StanderSorce> selectStanderSorce(StanderSorceExample standerSorceExample){
        return standerSorce_service.selectStanderSorce(standerSorceExample);
    }

    /**
     * 条件查询standerSorce
     * @param standerSorceExample
     * @param pageable 是否需要分页
     * @return
     */
    public List<StanderSorce> selectStanderScore(StanderSorceExample standerSorceExample, boolean pageable){
        if(! pageable){
            PageHelper.clearPage();
        }
        return standerSorce_service.selectStanderSorce(standerSorceExample);
    }

    /**
     * 保存standerSorce
     * @return
     */
    public boolean saveStanderSorce(StanderSorce standerSorce){
        int i = standerSorce_service.saveStanderSorce(standerSorce);
        if(i == 0){
            return false;
        }
        return true;
    }


    /**
     * 保存standerSorce
     * @return
     */
    public Integer saveStanderScore(StanderSorce standerSorce){
        return standerSorce_service.saveStanderScore(standerSorce);
    }
    /**
     * 保存standerSorce
     * @return
     */
    public boolean updateStanderSorceNotNull(StanderSorce standerSorce){
        int i = standerSorce_service.updateStanderSorceNotNull(standerSorce);
        if(i == 0){
            return false;
        }
        return true;
    }

    /**
     * 保存standerSorce
     * @return
     */
    public boolean updateStanderSorce(StanderSorce standerSorce){
        int i = standerSorce_service.updateStanderSorce(standerSorce);
        if(i == 0){
            return false;
        }
        return true;
    }

    /**
     * 删除standerSorce
     * @return
     */
    public boolean deleteStanderSorce(int standerSorceId){
        int i = standerSorce_service.deleteStanderSorce(standerSorceId);
        if(i == 0){
            return false;
        }
        return true;
    }

    public void standerSorceDelBeforeSave(StanderSorce standerSorce) {
        //TODO 需求待明确
//        StanderSorceExample standerSorceExample = new StanderSorceExample();
//        StanderSorceExample.Criteria criteria = standerSorceExample.createCriteria();
//        criteria.andUserIdEqualTo(standerSorce.getUserId());
//        standerSorce_service.deleteStanderSorceByParams(standerSorceExample);
        String scores = standerSorce.getScores();
        //处理scores
        String newScores  = scores.substring(1, scores.length()-1);
        standerSorce.setScores(newScores);
        saveStanderSorce(standerSorce);
    }

    public boolean updateFromDB(StanderSorce newStanderSorce) {
        if(null == newStanderSorce || null == newStanderSorce.getId()){
            return false;
        }
        StanderSorce oldStanderSorce = standerSorce_service.selectStanderSorceById(newStanderSorce.getId());
        if(null != oldStanderSorce){
            if(!StringUtil.isEmpty(newStanderSorce.getBkSchoolType())){
                oldStanderSorce.setBkSchoolType(newStanderSorce.getBkSchoolType());
            }
            if(!StringUtil.isEmpty(newStanderSorce.getBkEducationYear())){
                oldStanderSorce.setBkEducationYear(newStanderSorce.getBkEducationYear());
            }
            if(!StringUtil.isEmpty(newStanderSorce.getBkSpecial())){
                oldStanderSorce.setBkSpecial(newStanderSorce.getBkSpecial());
            }
            if(!StringUtil.isEmpty(newStanderSorce.getBkRank())){
                oldStanderSorce.setBkRank(newStanderSorce.getBkRank());
            }
            if(!StringUtil.isEmpty(newStanderSorce.getYjsSchoolType())){
                oldStanderSorce.setYjsSchoolType(newStanderSorce.getYjsSchoolType());
            }
            if(!StringUtil.isEmpty(newStanderSorce.getYjsSpecail())){
                oldStanderSorce.setYjsSpecail(newStanderSorce.getYjsSpecail());
            }
            if(!StringUtil.isEmpty(newStanderSorce.getYjsRank())){
                oldStanderSorce.setYjsRank(newStanderSorce.getYjsRank());
            }
            if(!StringUtil.isEmpty(newStanderSorce.getYjsEducationYear())){
                oldStanderSorce.setYjsEducationYear(newStanderSorce.getYjsEducationYear());
            }
            if(!StringUtil.isEmpty(newStanderSorce.getScores())){
                oldStanderSorce.setScores(newStanderSorce.getScores().substring(1, newStanderSorce.getScores().length()-1));
            }
            if(!StringUtil.isEmpty(newStanderSorce.getOtherInstructions())){
                oldStanderSorce.setOtherInstructions(newStanderSorce.getOtherInstructions());
            }
            if(!StringUtil.isEmpty(newStanderSorce.getLeaveMessage())){
                oldStanderSorce.setLeaveMessage(newStanderSorce.getLeaveMessage());
            }
            int flag = standerSorce_service.updateStanderSorce(oldStanderSorce);
            return flag > 0;

        }
        return false;
    }

    public List<StanderSorce> standerSorceQuery(Integer userId) {
        if(null != userId){
            StanderSorceExample standerSorceExample = new StanderSorceExample();
            StanderSorceExample.Criteria criteria = standerSorceExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            return standerSorce_service.selectStanderSorce(standerSorceExample);
        }else {
            return null;
        }
    }
}
